<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="1000px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="invFormRef" :model="state.invBasDoc" size="default">
        <el-row>
          <el-col :span="6">
            <el-form-item label="存货编码">
              <el-input v-model="state.invBasDoc.invcode" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="存货名称">
              <el-input v-model="state.invBasDoc.invname" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规格">
              <el-input v-model="state.invBasDoc.invspec" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="型号">
              <el-input v-model="state.invBasDoc.invtype" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="存货分类">
              <el-input v-model="state.invBasDoc.invclName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="计量单位">
              <el-select
                v-model="state.invBasDoc.pkMeasdoc"
                filterable
                placeholder="Select"
              >
                <el-option
                  v-for="meas in state.measList"
                  :key="meas.pkMeasdoc"
                  :label="meas.measName"
                  :value="meas.pkMeasdoc"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="仓库">
              <el-select v-model="state.invBasDoc.defaultWhPk" filterable>
                <el-option
                  v-for="meas in state.whList"
                  :key="meas.pkWh"
                  :label="meas.whName"
                  :value="meas.pkWh"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="备注">
              <el-input v-model="state.invBasDoc.remark" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-checkbox v-model="state.invBasDoc.isSelf" label="是否自制" />
          </el-col>
          <el-col :span="6">
            <el-checkbox v-model="state.invBasDoc.isComsume" label="生产耗用" />
          </el-col>
          <el-col :span="6">
            <el-checkbox
              v-model="state.invBasDoc.isShoujian"
              label="是否首检"
            />
          </el-col>
          <el-col :span="6">
            <el-checkbox v-model="state.invBasDoc.isSale" label="是否销售" />
          </el-col>
          <el-col :span="6">
            <el-checkbox
              v-model="state.invBasDoc.patrolInspect"
              label="是否巡检"
            />
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveInv">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import {
    getInventoryInfoApi,
    updateInventoryInfoApi,
    addInventoryInfoApi,
  } from '@/api/base/inventory/index'
  import { getWarehouseListApi } from '@/api/base/wh'
  // import { getDeptListApi } from '@/api/base/dept'
  import { getAllMeasdocApi } from '@/api/base/measdoc'
  import { ref } from 'vue'
  const $baseMessage = inject('$baseMessage')
  import { localStorageConfig } from '@/config/localstorage.config'

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const dialogTitle = '存货档案基本页'
  const dialogVisible = ref(false)
  const invFormRef = ref()
  // const xuanzeDingeDialogRef = ref()
  const state = reactive({
    pkInv: '',
    invBasDoc: {},
    whList: [],
    measList: [],
    action: 'add',
  })

  // const openXuanzeDingeDialog = () => {
  //   xuanzeDingeDialogRef.value.openQueryDinge()
  // }

  const toOpenInvInfoDialog = async (pkInv, pkInvcl, invClName) => {
    dialogVisible.value = true
    getAllWh()
    getAllMeas()
    if (pkInv) {
      state.pkInv = pkInv
      getInvInfo()
      state.action = 'edit'
    } else {
      state.invBasDoc = {}
      state.invBasDoc.invclName = invClName
      state.invBasDoc.pkInvcl = pkInvcl
      state.invBasDoc.pkCorp = pkCorp
      state.action = 'add'
    }
  }
  const getInvInfo = async () => {
    const params = {
      pkInv: state.pkInv,
    }
    const { code, data } = await getInventoryInfoApi(params)
    if (code === 2000) {
      state.invBasDoc = data
    }
  }
  const getAllWh = async () => {
    const { code, data } = await getWarehouseListApi({ pkCorp: pkCorp })
    if (code === 2000) {
      state.whList = data
    }
  }

  const getAllMeas = async () => {
    const params = {
      pkCorp: pkCorp,
    }
    const { code, data } = await getAllMeasdocApi(params)
    if (code === 2000) {
      state.measList = data
    }
  }

  const saveInv = async () => {
    if ('add' === state.action) {
      const { code } = await addInventoryInfoApi(state.invBasDoc)
      if (code === 2000) {
        $baseMessage('保存成功', 'success')
      } else {
        $baseMessage('保存失败', 'error')
      }
    } else if ('edit' === state.action) {
      const { code } = await updateInventoryInfoApi(state.invBasDoc)
      if (code === 2000) {
        $baseMessage('保存成功', 'success')
      }
    }
    _emits('save-callback')
    dialogVisible.value = false
  }

  defineExpose({ toOpenInvInfoDialog })
  const _emits = defineEmits(['save-callback'])
</script>

<style scoped>
  .el-form-item {
    margin-right: 20px;
  }
</style>
